package cn.flying.system.web.cm;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.base.common.vo.PageRequest;
import cn.flying.base.common.vo.PageResponse;
import cn.flying.base.common.vo.Rt;
import cn.flying.base.common.vo.TreeSelectInfo;
import cn.flying.system.common.vo.UserRelationInfoVo;
import cn.flying.system.domain.cm.CmPositionUser;
import cn.flying.system.domain.cm.CmUser;
import cn.flying.system.domain.cm.CmUserRole;
import cn.flying.system.service.cm.CmCodeService;
import cn.flying.system.service.cm.CmOrganizationService;
import cn.flying.system.service.cm.CmPositionService;
import cn.flying.system.service.cm.CmRoleService;
import cn.flying.system.service.cm.CmUserService;

/**
 * @description: 系统用户信息请求处理器
 * @author: lvyong
 * @date: 2022年05月22日 17:52
 * @version: 1.0
 */
@Controller
@RequestMapping("user")
public class CmUserController {

    @Autowired
    private CmUserService cmUserService;
    @Autowired
    private CmCodeService cmCodeService;
    @Autowired
    private CmOrganizationService cmOrganizationService;
    @Autowired
    private CmPositionService cmPositionService;
    @Autowired
    private CmRoleService cmRoleService;

    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("list")
    public String list() {
        return "sys/user/list";
    }

    /**
     * 加载列表数据
     *
     * @return
     */
    @RequestMapping(value = "getUserPageList", method = RequestMethod.POST)
    @ResponseBody
    public PageResponse<CmUser> getUserPageList(PageRequest<CmUser> pageRequest) {
        PageResponse<CmUser> pageResponse = cmUserService.getUserPageList(pageRequest);
        return pageResponse;
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/turn/edit")
    public String turnEdit(Long id, Model model) {
        Map<String, String> genderList = cmCodeService.getCodeList("gender");
        model.addAttribute("genderList", genderList);
        List<TreeSelectInfo> treeList = cmOrganizationService.getOrganizationTree();
        model.addAttribute("treeList", treeList);
        if (null != id) {
            CmUser cmUser = cmUserService.getUserById(id);
            model.addAttribute("user", cmUser);
            List<CmPositionUser> list = cmPositionService.getPositionUserList(cmUser.getUserId());
            if (CollectionUtils.isEmpty(list)) {
                model.addAttribute("positions", new ArrayList<String>());
            } else {
                model.addAttribute("positions", list.stream().map(p -> p.getPostId()).collect(Collectors.toList()));
            }

            List<CmUserRole> roles = cmRoleService.getUserRoleList(cmUser.getUserId());
            if (CollectionUtils.isEmpty(roles)) {
                model.addAttribute("roles", new ArrayList<String>());
            } else {
                model.addAttribute("roles", roles.stream().map(p -> p.getRoleId()).collect(Collectors.toList()));
            }
        } else {
            model.addAttribute("user", new CmUser());
        }
        return "sys/user/edit";
    }

    /**
     * 保存
     *
     * @param userRelationInfoVo
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Rt save(@RequestBody UserRelationInfoVo userRelationInfoVo) {
        if (userRelationInfoVo.getId() == null) {
            cmUserService.insertUser(userRelationInfoVo);
        } else {
            cmUserService.updateUserById(userRelationInfoVo);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "del/{userId}", method = RequestMethod.POST)
    @ResponseBody
    public Rt del(@PathVariable("userId") String userId) {
        cmUserService.deleteUserByUserId(userId);
        return Rt.success("删除成功！");
    }

}

